const mongoose = require('mongoose');

// 默认连接（two 数据库）
const defaultDB = mongoose.connect(
  'mongodb+srv://hli239064:kYmQRhL1mBZBgqpi@cluster0.plcz7oy.mongodb.net/two',
  { serverSelectionTimeoutMS: 30000 }
);

// 独立连接（Warehouse 数据库）
const warehouseDB = mongoose.createConnection(
  'mongodb+srv://hli239064:kYmQRhL1mBZBgqpi@cluster0.plcz7oy.mongodb.net/Warehouse',
  { serverSelectionTimeoutMS: 30000 }
);

// 监听连接事件
defaultDB
  .then(() => console.log('默认数据库（two）连接成功'))
  .catch(err => console.log('默认数据库（two）连接失败:', err.message));

  // 监听连接成功
  warehouseDB.on('open', () => {
    console.log('Warehouse 数据库连接成功');
  });
  
  // 监听连接失败
  warehouseDB.on('error', (err) => {
    console.log('Warehouse 数据库连接失败:', err.message);
  });

module.exports = { mongoose, warehouseDB };
